package com.owulia.leetcode.num.linked.list;

public class FindLoop {

    public static boolean hasCycle(ListNode head) {
        if (null == head || null == head.next) {
            return false;
        }

        ListNode fast = head;
        ListNode slow = head;
        while (null != slow && null != fast) {
            slow = slow.next;
            fast = fast.next;
            if (null != fast) {
                fast = fast.next;
            }
            if (fast == slow) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        head.next = new ListNode(2);
        head.next.next = new ListNode(3);
        head.next.next.next = new ListNode(4);
        head.next.next.next.next = head;
        System.out.print("\t" + hasCycle(head));
    }
}
